package com.youkeyi.ddy.cloud.application.business.hr.employee.work.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.work.BusinessEmployeeWorkExperienceCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.work.dto.BusinessEmployeeWorkExperienceListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.work.dto.BusinessEmployeeWorkExperienceListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.work.param.BusinessEmployeeWorkExperienceCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.work.param.BusinessEmployeeWorkExperienceDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.work.param.BusinessEmployeeWorkExperienceListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.work.param.BusinessEmployeeWorkExperienceUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.HrEmployeeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.param.HrEmployeeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.work.HrEmployeeWorkExperienceCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.work.dto.HrEmployeeWorkExperienceListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.work.dto.HrEmployeeWorkExperienceListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.work.param.HrEmployeeWorkExperienceCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.work.param.HrEmployeeWorkExperienceDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.work.param.HrEmployeeWorkExperienceQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.work.param.HrEmployeeWorkExperienceUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * BusinessEmployeeWorkExperienceController
 *
 * @Author liuxiawang
 * @Date 2024/4/30 10:16 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessEmployeeWorkExperienceController implements BusinessEmployeeWorkExperienceCloudService {
    
    @Autowired
    private HrEmployeeWorkExperienceCloudService hrEmployeeWorkExperienceCloudService;
    @Autowired
    private HrEmployeeCloudService hrEmployeeCloudService;

    @Override
    public BusinessEmployeeWorkExperienceListCloudDto list(BusinessEmployeeWorkExperienceListCloudParam param) {
        BusinessEmployeeWorkExperienceListCloudDto dto = new BusinessEmployeeWorkExperienceListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (StringUtils.isNotBlank(param.getNameFuzzy()) || CollectionUtils.isNotEmpty(param.getEmployeeCodes())) {
            HrEmployeeQueryCloudParam employeeQueryCloudParam = new HrEmployeeQueryCloudParam();
            employeeQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
            employeeQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
            employeeQueryCloudParam.setEmployeeCodes(param.getEmployeeCodes());
            employeeQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
            HrEmployeeListCloudDto employeeDtos = hrEmployeeCloudService.query(employeeQueryCloudParam);
            if (null == employeeDtos || CollectionUtils.isEmpty(employeeDtos.getRows())) {
                dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
                return dto;
            }

            param.getEmployeePrimaryKeys().clear();
            param.getEmployeePrimaryKeys().addAll(employeeDtos.getRows().stream().map(HrEmployeeListItemCloudDto::getEmployeePrimaryKey).collect(Collectors.toList()));
        }

        HrEmployeeWorkExperienceQueryCloudParam employeeQueryCloudParam = new HrEmployeeWorkExperienceQueryCloudParam();
        employeeQueryCloudParam.setEmployeeWorkExperiencePrimaryKeys(param.getEmployeeWorkExperiencePrimaryKeys());
        employeeQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
        employeeQueryCloudParam.setPageSize(param.getPageSize());
        employeeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        HrEmployeeWorkExperienceListCloudDto employeeDtos = hrEmployeeWorkExperienceCloudService.query(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            HrEmployeeQueryCloudParam eqcp = new HrEmployeeQueryCloudParam();
            eqcp.setEmployeePrimaryKeys(employeeDtos.getRows().stream().map(HrEmployeeWorkExperienceListItemCloudDto::getEmployeeKey).collect(Collectors.toList()));
            HrEmployeeListCloudDto eqcpDtos = hrEmployeeCloudService.query(eqcp);
            Map<String, HrEmployeeListItemCloudDto> eMap = new HashMap<>();
            if (null != eqcpDtos && CollectionUtils.isNotEmpty(eqcpDtos.getRows())) {
                eMap.putAll(eqcpDtos.getRows().stream().collect(Collectors.toMap(HrEmployeeListItemCloudDto::getEmployeePrimaryKey, Function.identity(), (k1, k2) -> k1)));
            }

            List<BusinessEmployeeWorkExperienceListItemCloudDto> items = new ArrayList<>();
            employeeDtos.getRows().forEach(employee -> {
                BusinessEmployeeWorkExperienceListItemCloudDto item = new BusinessEmployeeWorkExperienceListItemCloudDto();
                item.setEmployeeWorkExperiencePrimaryKey(employee.getEmployeeWorkExperiencePrimaryKey());
                item.setEmployeeKey(employee.getEmployeeKey());
                if (eMap.containsKey(employee.getEmployeeKey())) {
                    HrEmployeeListItemCloudDto e = eMap.get(employee.getEmployeeKey());
                    item.setEmployeeName(e.getName());
                    item.setEmployeeCode(e.getEmployeeCode());
                }
                item.setStartDate(employee.getStartDate());
                item.setEndDate(employee.getEndDate());
                item.setCompanyName(employee.getCompanyName());
                item.setPosition(employee.getPosition());
                item.setSalary(employee.getSalary());
                item.setJobDescription(employee.getJobDescription());
                item.setResignationReason(employee.getResignationReason());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime());
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime());
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(employeeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessEmployeeWorkExperienceCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeWorkExperienceCreateCloudParam employeeCreateCloudParam = new HrEmployeeWorkExperienceCreateCloudParam();
        employeeCreateCloudParam.setEmployeeKey(param.getEmployeeKey());
        employeeCreateCloudParam.setStartDate(param.getStartDate());
        employeeCreateCloudParam.setEndDate(param.getEndDate());
        employeeCreateCloudParam.setCompanyName(param.getCompanyName());
        employeeCreateCloudParam.setPosition(param.getPosition());
        employeeCreateCloudParam.setSalary(param.getSalary());
        employeeCreateCloudParam.setJobDescription(param.getJobDescription());
        employeeCreateCloudParam.setResignationReason(param.getResignationReason());
        employeeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeWorkExperienceCloudService.create(employeeCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessEmployeeWorkExperienceUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeWorkExperiencePrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeWorkExperienceUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeWorkExperienceUpdateCloudParam();
        employeeUpdateCloudParam.setEmployeeWorkExperiencePrimaryKey(param.getEmployeeWorkExperiencePrimaryKey());
        employeeUpdateCloudParam.setStartDate(param.getStartDate());
        employeeUpdateCloudParam.setEndDate(param.getEndDate());
        employeeUpdateCloudParam.setCompanyName(param.getCompanyName());
        employeeUpdateCloudParam.setPosition(param.getPosition());
        employeeUpdateCloudParam.setSalary(param.getSalary());
        employeeUpdateCloudParam.setJobDescription(param.getJobDescription());
        employeeUpdateCloudParam.setResignationReason(param.getResignationReason());
        employeeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeWorkExperienceCloudService.update(employeeUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessEmployeeWorkExperienceDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getEmployeeWorkExperiencePrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeWorkExperienceDeleteCloudParam employeeDeleteCloudParam = new HrEmployeeWorkExperienceDeleteCloudParam();
        employeeDeleteCloudParam.setEmployeeWorkExperiencePrimaryKeys(param.getEmployeeWorkExperiencePrimaryKeys());
        employeeDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeWorkExperienceCloudService.delete(employeeDeleteCloudParam);
    }
}
